﻿namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Net;
    using System.Threading;
    using System.Web;
    using System.Web.Mvc;
    using Moq;
    using Xunit;

    public sealed class MovedPermanentlyResultFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<MovedPermanentlyResult>()
                .DerivesFrom<RedirectionResult>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new MovedPermanentlyResult());
        }

        [Fact]
        public void ctor_stringNull()
        {
            Assert.Throws<ArgumentNullException>(() => new MovedPermanentlyResult(null));
        }

        [Fact]
        public void ctor_stringEmpty()
        {
            Assert.Throws<ArgumentOutOfRangeException>(() => new MovedPermanentlyResult(string.Empty));
        }

        [Fact]
        public void ctor_string()
        {
            Assert.NotNull(new MovedPermanentlyResult("test"));
        }

        [Fact]
        public void prop_Location()
        {
            Assert.True(new PropertyExpectations(typeof(MovedPermanentlyResult).GetProperty("Location"))
                .IsAutoProperty<string>("/")
                .Set("/default.html")
                .Result);
        }

        [Fact]
        public void op_ExecuteResult_FilterExecutingContextNull()
        {
            Assert.Throws<ArgumentNullException>(() => new MovedPermanentlyResult("/default.html").ExecuteResult(null));
        }

        [Fact]
        public void op_ExecuteResult_FilterExecutingContext()
        {
            var mock = new Mock<HttpContextBase>();
            mock.Setup(x => x.Request.Url).Returns(new Uri("http://example.com")).Verifiable();
            mock.Setup(x => x.Response.Cache.SetCacheability(HttpCacheability.Public)).Verifiable();
            mock.Setup(x => x.Response.Cache.SetExpires(It.IsAny<DateTime>())).Verifiable();
            mock.SetupSet(x => x.Response.StatusCode).Callback(expected => Assert.Equal<int>((int)HttpStatusCode.MovedPermanently, expected)).Verifiable();
            mock.Setup(x => x.Response.AppendHeader("Location", "http://example.com/default.html")).Verifiable();
            mock.SetupSet(x => x.Response.ContentType).Callback(expected => Assert.Equal<string>("text/html", expected)).Verifiable();
            mock.Setup(x => x.Response.Write(It.IsAny<string>())).Verifiable();

            new MovedPermanentlyResult("/default.html").ExecuteResult(new ControllerContext()
            {
                HttpContext = mock.Object
            });

            mock.VerifyAll();
        }

        [Fact]
        public void op_ExecuteResult_FilterExecutingContextAbsolute()
        {
            var mock = new Mock<HttpContextBase>();
            mock.Setup(x => x.Response.Cache.SetCacheability(HttpCacheability.Public)).Verifiable();
            mock.Setup(x => x.Response.Cache.SetExpires(It.IsAny<DateTime>())).Verifiable();
            mock.SetupSet(x => x.Response.StatusCode).Callback(expected => Assert.Equal<int>((int)HttpStatusCode.MovedPermanently, expected)).Verifiable();
            mock.Setup(x => x.Response.AppendHeader("Location", "http://example.com/default.html")).Verifiable();
            mock.SetupSet(x => x.Response.ContentType).Callback(expected => Assert.Equal<string>("text/html", expected)).Verifiable();
            mock.Setup(x => x.Response.Write(It.IsAny<string>())).Verifiable();

            new MovedPermanentlyResult("http://example.com/default.html").ExecuteResult(new ControllerContext()
            {
                HttpContext = mock.Object
            });

            mock.VerifyAll();
        }
    }
}